<template>
	<view class="page">
		<u-gap height="30"></u-gap>
		<view class="box flex-between align-center margin0 padding-row20">
			<view class="label">当前会员级别</view>
			<view class="value">{{ user.membershipName? user.membershipName : '基础会员' }}</view>
		</view>
		<scroll-view scroll-y="true" class="scrollVue">
			<u-gap height="20"></u-gap>
			<view class="li relative" v-for="(item, index) in list" :key="index" :class="{active: selectObj.id==item.id}" @click="selectFun(item)">
				<view class="flex align-center">
					<image src="../../static/radio1.png" style="width: 40rpx; height: 40rpx;" v-if="selectObj.id==item.id"></image>
					<image src="../../static/radio0.png" style="width: 40rpx; height: 40rpx;" v-else></image>
					<view class="name">{{item.title.split("-")[0]}}-</view>
				</view>
				<view class="name" style="padding: 10rpx 0 0 50rpx;">{{ item.price }}元/年</view>
				<view class="descri">{{ item.description }}</view>
				<view class="price absolute">{{ item.price }}元/年</view>			
			</view>
		</scroll-view>
		
		
		<view class="botVue">
			<view class="btn margin0" @click="buyFun()">立即购买</view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data(){
			return {
				selectObj: {},
				list: []
			}
		},
		
		async onLoad() {
			await this.getList()
		},
		
		computed: {
			...mapState({
				user: state=> state.user
			})
		},
		
		methods: {
			// 会员列表
			async getList(){
				const res = await this.$request("/aiet-user-center/user/membership/allMemberships", {}, "GET")
				if(res.code===200) this.list = res.data
			},
			
			// 选择套餐
			selectFun(item){
				this.selectObj = item
				console.log(this.selectObj)
			},
			
			// 立即购买
			async buyFun(){
				if(!this.selectObj.id) return this.$u.toast("请选择会员等级")
				uni.showLoading()
				const res = await this.$request("/aiet-user-center/user/orders/wxPay", {
					productName: this.selectObj.title,
					orderAmount: this.selectObj.price,
					platform: "数字人-H5",
					productType: 2,
					membershipId: this.selectObj.id,
					manufacturer:1
				}, "POST", "application/json")
				if(res.code===200) {
					console.log(res.data)
					uni.hideLoading()
					let payment = res.data
					let that = this
					uni.requestPayment({
						timeStamp: payment.timeStamp,
						nonceStr: payment.nonceStr,
						package: payment.package,
						signType: payment.signType,
						paySign: payment.paySign,
						success(payRes) {
							that.$u.toast("购买成功")
							that.$store.dispatch("getUser")
						},
						fail(payErr) {
							console.log("支付失败", payErr)
						}
					})
				}else{
					uni.hideLoading()
					this.$u.toast(res.msg)
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.scrollVue {
		width: 100%;
		height: calc(100vh - 298rpx - var(--window-bottom) - var(--window-top));
	}
	.box {
		width: 690rpx;
		height: 100rpx;
		background: #fff;
		border-radius: 12rpx;
		.value {
			color: #18AB4E;
			font-weight: 600;
		}
	}
	.li {
		width: 690rpx;
		height: 200rpx;
		border-radius: 12rpx;
		background: #fff;
		padding: 30rpx;
		box-sizing: border-box;
		margin: 0 auto 20rpx;
		.price {
			width: 180rpx;
			height: 66rpx;
			line-height: 66rpx;
			text-align: center;
			background: #DCFCE7;
			color: #18AB4E;
			top: 0rpx;
			right: 0rpx;
			font-weight: 600;
			border-radius:  0 12rpx 0 12rpx;
		}
		.name {
			font-weight: 600;
			color: #000;
			padding-left: 10rpx;
		}
		.descri {
			color: #AEB1C4;
			padding: 10rpx 0 0 50rpx;
		}
	}
	.active {
		background: #F0FDF4;
		border: 1rpx solid #18AB4E;
	}
	.botVue {
		width: 750rpx;
		height: 138rpx;
		background: #f5f5f5;
		position: fixed;
		left: 0;
		bottom: 0rpx;
		z-index: 99;
		text {
			color: #888;
			line-height: 1;
		}
		.btn {
			width: 690rpx;
			height: 88rpx;
			background: #18AB4E;
			border-radius: 12rpx;
			line-height: 88rpx;
			text-align: center;
			color: #fff;
		}
	}
</style>